<?php

class Resource extends BaseModel 
{
    /**
     * @var int resource id
     * @soap
     */
    public $res_id;
    
    /**
     * @var string resource name
     * @soap
     */
    public $res_name;

    /**
     * @var string resource description
     * @soap
     */
    public $res_desc;
    
    /**
     * @var int resource frequency
     * @soap
     */
    public $res_frequency;

	/**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__)
    {
		return parent::model($className);
    }
	
	/**
     * @return string the associated database table name
     */
    public function tableName()
    {
		return 'resource';
    }

    public static function listItems($res_type=-1) 
    {
        if($res_type == -1) {
            $resources = Resource::model()->findAll();
        }
        else {
            $resources = Resource::model()->findAllByAttributes(array('res_type'=>$res_type));
        }
        return CHtml::listData($resources, 'res_id', 'res_name'); 
    }
     
    public function rules()
    {
        return array(
            array('res_name, product_id, res_type, res_frequency, quality_level', 'required', 'message'=>'{attribute} 不能为空'),
            array('res_name', 'unique'),
        );
    }

    /**
     *
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'res_id' => '编号',
            'res_name' => '名称',
            'product_id' => '产品线',
            'res_type' => '类型',
            'res_frequency' => '周期',
            'quality_level'   => '重要程度',
            'res_desc' =>  '描述',
        );
    }
	public function relations() 
	{
		return array(
			'jobs'	=>	array(self::MANY_MANY, 'Job', 'job_output(res_id, job_id)'),
			'product'	=>	array(self::BELONGS_TO, 'Product', 'product_id'),
		);
    }
    
    public function search() {
        $criteria=new CDbCriteria;
        $criteria->compare('job_type', $type);
        return new CActiveDataProvider('Job', array(
            'criteria'=>$criteria,
            'sort'=>array(
                'defaultOrder'=>'job_id DESC',
            ),
        ));
    }
}
